package com.fadadianqi.commons;

import com.yeqiangwei.commons.util.DateTimeUtils;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class PrimaryKeyCreator {

    public static void main(String[] args) {
        System.out.println(PrimaryKeyCreator.create());
    }

    private static final Lock lock = new ReentrantLock();
    private static String previousId = null;

    public static final String create() {
        lock.lock();
        String id = String.valueOf(DateTimeUtils.getTimeInMillis());
        lock.unlock();
        if (id.equals(previousId)) {
            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return create();
        }
        return previousId = id;
    }
}
